package com.atguigu.atcrowdfunding.manager.service.impl;

import com.atguigu.atcrowdfunding.bean.Member;
import com.atguigu.atcrowdfunding.bean.Permission;
import com.atguigu.atcrowdfunding.bean.Role;
import com.atguigu.atcrowdfunding.bean.User;
import com.atguigu.atcrowdfunding.exception.LoginFailException;
import com.atguigu.atcrowdfunding.exception.RegisterFailException;
import com.atguigu.atcrowdfunding.manager.dao.RoleMapper;
import com.atguigu.atcrowdfunding.manager.dao.UserMapper;
import com.atguigu.atcrowdfunding.manager.dao.UserRoleMapper;
import com.atguigu.atcrowdfunding.manager.service.UserService;
import com.atguigu.atcrowdfunding.util.Const;
import com.atguigu.atcrowdfunding.util.MD5Util;
import com.atguigu.atcrowdfunding.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author xzt
 * @create 2021-02-08 19:34
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    public User login(Map paramMap) {
        User user = userMapper.queryUserlogin(paramMap);

        if(user == null){
            throw new LoginFailException("用户账号或密码不正确!");
        }

        return user;
    }

    /**
     * 返回false表示用户不存在
     * @param loginacct
     * @return
     */
    public boolean isRegister(String loginacct) {

        Member member = userMapper.queryUserRegister(loginacct);

        if(member != null){
            throw new RegisterFailException("用户名已存在");
        }

        return false;
    }

    public int register(Member member) {
        return userMapper.insertMember(member);
    }

    @Override
    public Page queryPage(Map<String,Object> paramMap) {

        Page page = new Page((Integer) paramMap.get("pageNo"),(Integer) paramMap.get("pageSize"));

        Integer startIndex = page.startIndex();

        paramMap.put("startIndex",startIndex);

        List<User> datas = userMapper.queryList(paramMap);

        page.setDatas(datas);

        Integer totalSize = userMapper.queryCount(paramMap);

        page.setTotalSize(totalSize);

        return page;
    }

    @Override
    public int saveUser(User user) {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        user.setCreatetime(dateFormat.format(date));

        user.setUserpswd(MD5Util.digest(Const.PASSWORD));

        return userMapper.insert(user);
    }

    @Override
    public User getUserById(Integer id) {
        return userMapper.queryUserById(id);
    }

    @Override
    public User getUserByLoginacct(String loginacct) {
        return userMapper.queryUserByLoginacct(loginacct);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateByPrimaryKey(user);
    }

    @Override
    public int deleteUser(String loginacct) {
        return userMapper.deleteUserByLoginacct(loginacct);
    }

    @Override
    public int deleteUsers(int[] ids) {
        int count = userMapper.deleteByPrimaryKeys(ids);
        if(count != ids.length){
            throw new RuntimeException("批量删除失败");
        }
        return count;
    }

    @Override
    public List<Role> queryAllRole() {
        return roleMapper.selectAll();
    }

    @Override
    public List<Integer> queryRoleByUserId(Integer id) {
        return userRoleMapper.selectByUserId(id);
    }

    @Override
    public Integer addRoleById(Integer userid, Integer[] roleid) {
        return userRoleMapper.insertRoleById(userid,roleid);
    }

    @Override
    public Integer removeRoleById(Integer userid, Integer[] roleid) {
        return userRoleMapper.deleteRoleById(userid,roleid);
    }

    @Override
    public List<Permission> queryPermissionByUserId(Integer id) {

        return userMapper.queryPermissionByUserId(id);
    }

}
